package com.wlr.littleprogram.handler;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import com.wlr.littleprogram.annotation.TokenToUser;

/**
 * @author lrwang
 * @version 1.0
 * @date 2022/8/10 16:40
 */
@Component
public class TokenToUserArgumentResolver implements HandlerMethodArgumentResolver {
    /**
     * 参数存在走resolveArgument方法
     *
     * @param parameter
     * @return
     */
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.hasParameterAnnotation(TokenToUser.class)) {
            return true;
        }
        return false;
    }

    /**
     * 解析
     *
     * @param parameter
     * @param mavContainer
     * @param webRequest
     * @param binderFactory
     * @return
     * @throws Exception
     */
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        if (parameter.getParameterAnnotation(TokenToUser.class) instanceof TokenToUser) {
            String token = webRequest.getHeader("token");
            if (StringUtils.isNotEmpty(token) && token.length() == 32) {

            }
        }
        return null;
    }
}
